import java.util.Arrays;

public class MyStack {
    private int[] elem;
    private int usedSize;

    public MyStack(int initialCapacity) {
        this.elem = new int[initialCapacity];
    }
    public void push(int val) {
        if (isFull()) {
            elem = Arrays.copyOf(elem, elem.length * 2);
        }
        elem[usedSize] = val;
        usedSize++;
    }
    private boolean isFull() {
        return this.usedSize == this.elem.length;
    }
    public int pop() {
        if (isEmpty()) {
            throw new MyException("栈中无元素，无法删除");
        }
        int oldVal = this.elem[usedSize - 1];
        this.usedSize--;
        return oldVal;
    }
    public int peek() {
        if (isEmpty()) {
            throw new MyException("栈中无元素，无法获取");
        }
        return this.elem[usedSize - 1];
    }
    public boolean isEmpty() {
        return usedSize == 0;
    }
}
